local storyboard = require( "storyboard" )
	local scene = storyboard.newScene()
	local ui = require ("ui")
	local wEasing = require "wEasing"
	local gui = require "Stage5.Stage5HomeGui".new()
	system.activate("multitouch")
function scene:createScene(event)
	local group = self.view	
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	--local group = self.view

	-- audio.play( sfx.themeMusic, { onComplete = function() 
                                      -- audio.dispose( sfx.themeMusic ) 
                                      -- end } )
	-----------------------------------------------------------------------------
	local mainHomeGroup = self.view
	local arrayColor = {}
	local arrayButton = {}
	local arrayBtn = {}
	local arrayButtonColor ={}
	local arrayCount = {0,0,0}
	local arrayScore = {1100, 1000, 900, 800, 600}
	local arrayTimeScore = {}
	local timeReady = 1
	local deltaTime = 1600
	local time1 = 0
	local timeTotalScore = 0
	local countturn = 0
	local delay = 100
	local loop = 0
	local timeScore = 0
	local bg 
	local btnPause
	local keyListener = false
	local statusBoard = "start"
	--local txtCount = {}
	local iconGood, iconOk, iconPerfect
	local txtTimePlay, txtTimeReady, txtTimeScore
	local gameOver, win, go
	function listColor()
		arrayColor[1] = display.newImageRect(mainHomeGroup,gui.blue.fn, gui.blue.w, gui.blue.h)
		arrayColor[1].x = gui.blue.x
		arrayColor[1].y = gui.blue.y
		arrayColor[1].fn = gui.blue.fn
		arrayColor[1].w = gui.blue.w
		arrayColor[1].h = gui.blue.h
		arrayColor[2] = display.newImageRect(mainHomeGroup,gui.green.fn, gui.green.w, gui.green.h)
		arrayColor[2].x = gui.green.x
		arrayColor[2].y = gui.green.y
		arrayColor[2].fn = gui.green.fn
		arrayColor[2].w = gui.green.w
		arrayColor[2].h = gui.green.h
		arrayColor[3] = display.newImageRect(mainHomeGroup,gui.purple.fn, gui.purple.w, gui.purple.h)
		arrayColor[3].x = gui.purple.x
		arrayColor[3].y = gui.purple.y
		arrayColor[3].fn = gui.purple.fn
		arrayColor[3].w = gui.purple.w
		arrayColor[3].h = gui.purple.h
		arrayColor[4] = display.newImageRect(mainHomeGroup,gui.red.fn, gui.red.w, gui.red.h)
		arrayColor[4].x = gui.red.x
		arrayColor[4].y = gui.red.y
		arrayColor[4].fn = gui.red.fn
		arrayColor[4].w = gui.red.w
		arrayColor[4].h = gui.red.h
		arrayColor[5] = display.newImageRect(mainHomeGroup,gui.yellow.fn, gui.yellow.w, gui.yellow.h)
		arrayColor[5].x = gui.yellow.x
		arrayColor[5].y = gui.yellow.y
		arrayColor[5].fn = gui.yellow.fn
		arrayColor[5].w = gui.yellow.w
		arrayColor[5].h = gui.yellow.h
		-------------------- arrayButtonColor ---------------------------
		arrayButtonColor[1] = display.newImageRect(mainHomeGroup, gui.btnBlue.fn, gui.btnBlue.w, gui.btnBlue.h)
		arrayButtonColor[1].fn = gui.btnBlue.fn
		arrayButtonColor[1].w = gui.btnBlue.w
		arrayButtonColor[1].h = gui.btnBlue.h
		arrayButtonColor[2] = display.newImageRect(mainHomeGroup, gui.btnGreen.fn, gui.btnGreen.w, gui.btnGreen.h)
		arrayButtonColor[2].fn = gui.btnGreen.fn
		arrayButtonColor[2].w = gui.btnGreen.w
		arrayButtonColor[2].h = gui.btnGreen.h
		arrayButtonColor[3] = display.newImageRect(mainHomeGroup, gui.btnPurple.fn, gui.btnPurple.w, gui.btnPurple.h)
		arrayButtonColor[3].fn = gui.btnPurple.fn
		arrayButtonColor[3].w = gui.btnPurple.w
		arrayButtonColor[3].h = gui.btnPurple.h
		arrayButtonColor[4] = display.newImageRect(mainHomeGroup, gui.btnRed.fn, gui.btnRed.w, gui.btnRed.h)
		arrayButtonColor[4].fn = gui.btnRed.fn
		arrayButtonColor[4].w = gui.btnRed.w
		arrayButtonColor[4].h = gui.btnRed.h
		arrayButtonColor[5] = display.newImageRect(mainHomeGroup, gui.btnYellow.fn, gui.btnYellow.w, gui.btnYellow.h)
		arrayButtonColor[5].fn = gui.btnYellow.fn
		arrayButtonColor[5].w = gui.btnYellow.w
		arrayButtonColor[5].h = gui.btnYellow.h
		for i = 1,5 do
			arrayColor[i].alpha = 0
			arrayButtonColor[i].alpha = 0
		end
		a = 0
		for i = 1,3 do 
			arrayBtn[i] = display.newImageRect(mainHomeGroup,gui.btnBlue.fn, gui.btnBlue.w, gui.btnBlue.h)
			arrayBtn[i].x = gui.btnBlue.x+a
			arrayBtn[i].y = gui.btnBlue.y
			a = a+100
			arrayBtn[i].alpha = 0
		end
	end
	function viewColor()
		txtTimeScore.alpha = 0
		statusBoard = "play"
		random1 = math.random(1,5)
		for i = 1,5 do
			if i == random1 then
				arrayColor[i].alpha = 1
			end
		end
		----random vi tri của size khac
		random2 = math.random(1,3)
		if random2 == 1 then
			random3 = math.random(1,5)
			while random3 == random1 do
				random3 = math.random(1,5)
			end
			arrayCount[1] = 0
			arrayCount[2] = 1
			arrayCount[3] = 1
			arrayButton[1] = display.newImageRect(mainHomeGroup,arrayButtonColor[random3].fn, arrayButtonColor[random3].w, arrayButtonColor[random3].h)
			arrayButton[2] = display.newImageRect(mainHomeGroup,arrayButtonColor[random1].fn, arrayButtonColor[random1].w, arrayButtonColor[random1].h)
			arrayButton[3] = display.newImageRect(mainHomeGroup,arrayButtonColor[random1].fn, arrayButtonColor[random1].w, arrayButtonColor[random1].h)
		elseif random2 == 2 then
			random3 = math.random(1,5)
			while random3 == random1 do
				random3 = math.random(1,5)
			end
			arrayCount[1] = 1
			arrayCount[2] = 0
			arrayCount[3] = 1
			arrayButton[1] = display.newImageRect(mainHomeGroup,arrayButtonColor[random1].fn, arrayButtonColor[random1].w, arrayButtonColor[random1].h)
			arrayButton[2] = display.newImageRect(mainHomeGroup,arrayButtonColor[random3].fn, arrayButtonColor[random3].w, arrayButtonColor[random3].h)
			arrayButton[3] = display.newImageRect(mainHomeGroup,arrayButtonColor[random1].fn, arrayButtonColor[random1].w, arrayButtonColor[random1].h)
		else 
			arrayCount[1] = 1
			arrayCount[2] = 1
			arrayCount[3]= 0
			random3 = math.random(1,5)
			while random3 == random1 do
				random3 = math.random(1,5)
			end
			arrayButton[1] = display.newImageRect(mainHomeGroup,arrayButtonColor[random1].fn, arrayButtonColor[random1].w, arrayButtonColor[random1].h)
			arrayButton[2] = display.newImageRect(mainHomeGroup,arrayButtonColor[random1].fn, arrayButtonColor[random1].w, arrayButtonColor[random1].h)
			arrayButton[3] = display.newImageRect(mainHomeGroup,arrayButtonColor[random3].fn, arrayButtonColor[random3].w, arrayButtonColor[random3].h)
		end
		arrayButton[1].x = arrayBtn[1].x
		arrayButton[1].y = arrayBtn[1].y
		arrayButton[2].x = arrayBtn[2].x
		arrayButton[2].y = arrayBtn[2].y
		arrayButton[3].x = arrayBtn[3].x
		arrayButton[3].y = arrayBtn[3].y
		if keyListener == false then
			for i = 1,3 do
				arrayButton[i].id = i
				arrayButton[i]:addEventListener("touch", onTouchButton)
			end
			keyListener = true
		end
		-- for i = 1,3 do
			--txtCount[i].alpha = 1
			-- txtCount[i].text = arrayCount[i]
		-- end
	end
	function onReplayButtonTap()
		if keyListener == true then
			for k,v in pairs(arrayButton) do
				v:removeEventListener("touch", onTouchButton)
			end
			keyListener = false
		end
		Init()
	end
	onStartPopupPause = function()
		if(timer_countdown ~= nil)then
			timer.pause(timer_countdown)
		end
		if (timer_countdownReady ~= nil ) then
			timer.pause(timer_countdownReady)
		end
		if keyListener == true then
			for i = 1,3 do
				if arrayButton[i] ~= nil then
					arrayButton[i]:removeEventListener("touch", onTouchButton)
				end
			end
			keyListener = false
		end
	end
	onResumePopup = function()
		if(timer_countdown ~= nil)then
			timer.resume(timer_countdown)
		end
		if(timer_countdownReady ~= nil)then
			timer.resume(timer_countdownReady)
		end
		if keyListener == false and statusBoard == "play" then
			for i = 1,3 do
				if arrayButton[i] ~= nil then
					arrayButton[i]:addEventListener("touch", onTouchButton)
				end
			end
			keyListener = true
		end
	end
	function Pause()
		local g1 = require "popup.popupPause".new(onResumePopup,onReplayButtonTap)
			local group1 = display.newGroup()
			group1:insert(g1)
			mainHomeGroup:insert(group1)
			group1.y = -300
			transition.to(group1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic, onStart = onStartPopupPause})
		
	end
	function onTouchButton(event)
		phase = event.phase
		if phase == "began" then
			local t = event.target
			print (t.id)
			if t.id == arrayButton[1].id  then
				arrayCount[1] = arrayCount[1] - 1
				--txtCount[1].text = arrayCount[1]
			elseif t.id == arrayButton[2].id then
				arrayCount[2] = arrayCount[2] - 1
				--txtCount[2].text = arrayCount[2]
			elseif t.id == arrayButton[3].id then
				arrayCount[3] = arrayCount[3] - 1
				--txtCount[3].text = arrayCount[3]
			end
			checkWin()
			for i =1,3 do
				if arrayCount[i] < 0 then
					gameOver.alpha = 1
					if keyListener == true then
						for j = 1,3 do
							arrayButton[j]:removeEventListener("touch", onTouchButton)
						end
						keyListener = false
					end
					timer.cancel(timer_countdown)
					timer.cancel(timer_countdownReady)
				end
			end
		end
	end
	function finishedGame()
		if timeScore > 1.1 then
			numberStar = 0
		elseif timeScore == 1.1 then
			numberStar = 1
		elseif timeScore == 1.0 then
			numberStar = 2
		elseif timeScore == 0.9  then
			numberStar = 3
		elseif timeScore > 0.6 and timeScore <= 0.8 then
			numberStar = 4
		elseif timeScore <= 0.6 then
			numberStar = 5
		end
		local g1
			g1 = require "popup.popupWin".new(timeScore*1000,numberStar,arrayScore,onReplayButtonTap)
		transition.to(g1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic})
	end
	function checkWin()
		for i = 1,10 do
			arrayTimeScore[i] = 0
		end
		if arrayCount[1] == 0 and arrayCount[2] == 0 and arrayCount[3] == 0 then
			countturn = countturn+1
			arrayTimeScore[countturn] = (math.round((tonumber((deltaTime-time1)))/100))/10
			timer.cancel(timer_countdown)
			statusBoard = "wait"
			print("countturn = "..countturn)
			print (arrayTimeScore[countturn])
			for i = 1,10 do
				timeTotalScore = timeTotalScore + arrayTimeScore[i]
				i = i+1
			end
			timeScore = math.round((timeTotalScore/countturn)*10)/10
			txtTimeScore.text = timeScore
			txtTimePlay.alpha = 0
			for j = 1,3 do
				display.remove(arrayButton[j])
				keyListener = false
			end
			for i = 1,5 do
				arrayColor[i].alpha = 0
			end
			if countturn <  10 then
				timeReady = 1
				if arrayTimeScore[countturn] <= 0.6 then
					iconPerfect.alpha = 1
					iconGood.alpha = 0
					iconOk.alpha = 0
				elseif arrayTimeScore[countturn]>0.6 and arrayTimeScore[countturn] <=0.9 then
					iconGood.alpha = 1
					iconOk.alpha = 0
					iconPerfect.alpha = 0
				elseif arrayTimeScore[countturn]>0.9 and arrayTimeScore[countturn] <= 1.2 then
					iconOk.alpha = 1
					iconGood.alpha = 0
					iconPerfect.alpha = 0
				end
				--print (countturn)
				if countturn>5 and countturn <10 then
					deltaTime = deltaTime -100
				end
				--print("deltaTime = "..deltaTime)
				txtTimeReady.alpha = 0
				timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
			else
				timefinish = timer.performWithDelay(300,function()
																	finishedGame()
																end ,1)
			end
		end
	end
	function Init()
		txtTimeReady.text = "3"
		statusBoard = "start"
		txtTimeReady.alpha = 1
		txtTimePlay.alpha = 0
		deltaTime = 1600
		countturn = 0
		iconOk.alpha = 0
		iconGood.alpha = 0
		iconPerfect.alpha = 0
		gameOver.alpha = 0
		timeScore = 0
		timeTotalScore = 0
		txtTimeScore.text = timeScore
		for i = 1,3 do
			arrayCount[i] = 0
			--txtCount[i].text = arrayCount[i]
		end
		if timer_countdown ~= nil then
			timer.cancel(timer_countdown)
		end
		if timer_countdownReady ~= nil then
			timer.cancel(timer_countdownReady)
		end
		for j = 1,3 do
			if arrayButton[j] ~= nil then
				display.remove(arrayButton[j])
			end
		end
		for i = 1,5 do
			arrayColor[i].alpha = 0
		end
		timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
	end
	function countdownTime (event)
		txtTimePlay.alpha = 1
		time1 = time1 - 100
		 txtTimePlay.text = (math.round((time1)/100))/10 ..""
		 if( time1 < 0) then
			  txtTimePlay.text = "0"
			  timer.cancel(timer_countdown)
			  mainHomeGroup:insert(gameOver)
			  gameOver.alpha = 1
			if keyListener == true then
				for j = 1,3 do
					arrayButton[j]:removeEventListener("touch", onTouchButton)
				end
				keyListener = false
			end
		end
	end
	function countdownReady ()
		timeReady = timeReady - 1
		txtTimeReady.text = timeReady
			if txtTimeReady ~= nil and statusBoard== "start" then
				--txtTimeReady.alpha = 1
			end
		if timeReady <1 then
			iconOk.alpha = 0
			iconGood.alpha = 0
			iconPerfect.alpha = 0
			txtTimeReady.alpha = 0
			go.alpha = 1
		end
		if timeReady <0 then
			txtTimeReady.alpha = 0
			go.alpha = 0
			time1 = deltaTime
			timer.cancel(timer_countdownReady)
			viewColor()
			txtTimePlay.alpha = 1
			timer_countdown = timer.performWithDelay(delay, countdownTime, loop)
			timeSystem = system.getTimer()
		end
	end
	timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
	bg = display.newImageRect(mainHomeGroup, gui.bg.fn, gui.bg.w, gui.bg.h)
		bg.x = gui.bg.x
		bg.y = gui.bg.y
	btnPause = ui.newButton({ defaultSrc = gui.btnPause.fn,
											overSrc = gui.btnPause.fn,
											defaultX = gui.btnPause.w,
											defaultY = gui.btnPause.h,
											overX = gui.btnPause.w,
											overY = gui.btnPause.h,
											overAlpha = gui.btnPause.overAlpha,
											overScale = gui.btnPause.overScale,
											x = gui.btnPause.x,
											y = gui.btnPause.y,
											onRelease = Pause
										})
		mainHomeGroup:insert(btnPause)
	txtTimeReady = display.newText(mainHomeGroup, timeReady,gui.txtTimeReady.x , gui.txtTimeReady.y, gui.txtTimeReady.font, gui.txtTimeReady.size)
		txtTimeReady:setTextColor(unpack(gui.txtTimeReady.color))
		txtTimeReady.alpha = 0
	txtTimePlay = display.newText(mainHomeGroup, deltaTime/1000,gui.txtTimePlay.x -20, gui.txtTimePlay.y, gui.txtTimePlay.font, gui.txtTimeReady.size)
		txtTimePlay:setTextColor(unpack(gui.txtTimePlay.color))
		txtTimePlay.alpha = 0
	go = display.newImageRect(mainHomeGroup, gui.go.fn, gui.go.w, gui.go.h)
		go.x = gui.go.x
		go.y = gui.go.y
		go.alpha = 0
	txtTimeScore = display.newText(mainHomeGroup,timeScore, gui.txtTimeScore.x, gui.txtTimeScore.y, gui.txtTimeScore.font, gui.txtTimeScore.size)
		txtTimeScore:setTextColor(unpack(gui.txtTimeScore.color))
		txtTimeScore.alpha = 0
	gameOver = display.newText(mainHomeGroup,"GAME OVER", gui.txtTimeReady.x, gui.txtTimeReady.y+30, gui.txtTimeReady.font, gui.txtTimeReady.size)
		gameOver.x = display.contentCenterX
		gameOver:setTextColor(255,0,0)
		gameOver.alpha = 0
	iconOk = display.newImageRect(mainHomeGroup, gui.iconOk.fn, gui.iconOk.w, gui.iconOk.h)
		iconOk.x = gui.iconOk.x
		iconOk.y = gui.iconOk.y
		iconOk.alpha= 0
	iconGood = display.newImageRect(mainHomeGroup, gui.iconGood.fn, gui.iconGood.w, gui.iconGood.h)
		iconGood.x = gui.iconGood.x
		iconGood.y = gui.iconGood.y
		iconGood.alpha= 0
	iconPerfect = display.newImageRect(mainHomeGroup, gui.iconPerfect.fn, gui.iconPerfect.w, gui.iconPerfect.h)
		iconPerfect.x = gui.iconPerfect.x
		iconPerfect.y = gui.iconPerfect.y
		iconPerfect.alpha= 0
	win = display.newText(mainHomeGroup,"YOU WIN", gui.txtTimeReady.x, 150, gui.txtTimeReady.font, gui.txtTimeReady.size+20)
		win.x = display.contentCenterX
		win.y = display.contentCenterY
		win:setTextColor(unpack(gui.txtTimeReady.color))
		win.alpha = 0
	-- txtCount[1] = display.newText(mainHomeGroup,arrayCount[1], 95, 380, native.systemFont,20)
		-- txtCount[1]:setTextColor(0,0,0)
	-- txtCount[2] = display.newText(mainHomeGroup,arrayCount[2],195,380,native.systemFont,20)
		-- txtCount[2]:setTextColor(0,0,0)
	-- txtCount[3] = display.newText(mainHomeGroup,arrayCount[3],295,380,native.systemFont,20)
		-- txtCount[3]:setTextColor(0,0,0)
	-- for i = 1,3 do
		-- txtCount[i].alpha = 0
	-- end
	
	listColor()
	return mainHomeGroup
	
	-----------------------------------------------------------------------------
	
end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
	
	-----------------------------------------------------------------------------
	
end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. remove listeners, widgets, save state, etc.)
	
	-----------------------------------------------------------------------------
	
end

---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

---------------------------------------------------------------------------------

return scene
